package com.crazy.rest;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.tomcat.util.ExceptionUtils;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/11/18 13:56
 */
@ApiModel("标准Rest返回结果")
public class RestResult<TData> extends BaseResult {
    @ApiModelProperty(
            value = "REST请求返回数据",
            dataType = "Object"
    )
    protected TData data;
    @ApiModelProperty(
            value = "REST请求返回额外数据",
            dataType = "Object"
    )
    protected Object extendData;

    public RestResult() {
        this.setStatus(1);
    }

    public void setResult(int status, String message) {
        this.setStatus(status);
        this.setMessage(message);
    }

    public void setResult(int status, String message, TData data) {
        this.setResult(status, message);
        this.setData(data);
    }

    public void setResult(int status, String message, TData data, Object extendData) {
        this.setResult(status, message, data);
        this.setExtendData(extendData);
    }

    public void setSuccessResult(String message) {
        this.setResult(1, message, (TData)null, (Object)null);
    }

    public void setSuccessResult(String message, TData data) {
        this.setResult(1, message, data, (Object)null);
    }

    public void setSuccessResult(String message, TData data, Object extendData) {
        this.setResult(1, message, data, extendData);
    }

    public void setFailedResult(String message) {
        this.setResult(0, message, (TData)null, (Object)null);
    }

    public void setFailedResult(String message, TData data) {
        this.setResult(0, message, data, (Object)null);
    }

    public void setFailedResult(String message, TData data, Object extendData) {
        this.setResult(0, message, data, extendData);
    }

    public void setErrorResult(String message) {
        this.setResult(-1, message, (TData)null, (Object)null);
    }

    public void setErrorResult(String message, TData data) {
        this.setResult(-1, message, data, (Object)null);
    }

    public void setErrorResult(String message, TData data, Object extendData) {
        this.setResult(-1, message, data, extendData);
    }

    public static RestResult createSuccessResult(String message) {
        RestResult restResult = new RestResult();
        restResult.setSuccessResult(message);
        return restResult;
    }

    public static RestResult createSuccessResult(String message, Object data) {
        RestResult restResult = new RestResult();
        restResult.setSuccessResult(message, data);
        return restResult;
    }

    public static RestResult createSuccessResult(String message, Object data, Object extendData) {
        RestResult restResult = new RestResult();
        restResult.setSuccessResult(message, data, extendData);
        return restResult;
    }

    public static RestResult createFailedResult(String message) {
        RestResult restResult = new RestResult();
        restResult.setFailedResult(message);
        return restResult;
    }

    public static RestResult createFailedResult(String message, Object data) {
        RestResult restResult = new RestResult();
        restResult.setFailedResult(message, data);
        return restResult;
    }

    public static RestResult createFailedResult(String message, Object data, Object extendData) {
        RestResult restResult = new RestResult();
        restResult.setFailedResult(message, data, extendData);
        return restResult;
    }

    public static RestResult createErrorResult(String message) {
        RestResult restResult = new RestResult();
        restResult.setErrorResult(message);
        return restResult;
    }

    public static RestResult createErrorResult(String message, Object data) {
        RestResult restResult = new RestResult();
        restResult.setErrorResult(message, data);
        return restResult;
    }

    public static RestResult createErrorResult(String message, Object data, Object extendData) {
        RestResult restResult = new RestResult();
        restResult.setErrorResult(message, data, extendData);
        return restResult;
    }

    public static RestResult createExceptionResult(String message, Exception ex) {
        RestResult restResult = new RestResult();
        restResult.setErrorResult(message, (Object)null, ExceptionUtils.unwrapInvocationTargetException(ex));
        return restResult;
    }

    public static RestResult createExceptionResult(String message, Object data, Exception ex) {
        RestResult restResult = new RestResult();
        restResult.setErrorResult(message, (Object)null, ExceptionUtils.unwrapInvocationTargetException(ex));
        return restResult;
    }

    public TData getData() {
        return this.data;
    }

    public Object getExtendData() {
        return this.extendData;
    }

    public void setData(final TData data) {
        this.data = data;
    }

    public void setExtendData(final Object extendData) {
        this.extendData = extendData;
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof RestResult)) {
            return false;
        } else {
            RestResult<?> other = (RestResult)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$data = this.getData();
                Object other$data = other.getData();
                if (this$data == null) {
                    if (other$data != null) {
                        return false;
                    }
                } else if (!this$data.equals(other$data)) {
                    return false;
                }

                Object this$extendData = this.getExtendData();
                Object other$extendData = other.getExtendData();
                if (this$extendData == null) {
                    if (other$extendData != null) {
                        return false;
                    }
                } else if (!this$extendData.equals(other$extendData)) {
                    return false;
                }

                return true;
            }
        }
    }

    @Override
    protected boolean canEqual(final Object other) {
        return other instanceof RestResult;
    }

    @Override
    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        Object $data = this.getData();
        result = result * 59 + ($data == null ? 43 : $data.hashCode());
        Object $extendData = this.getExtendData();
        result = result * 59 + ($extendData == null ? 43 : $extendData.hashCode());
        return result;
    }

    @Override
    public String toString() {
        return "RestResult(data=" + this.getData() + ", extendData=" + this.getExtendData() + ")";
    }
}
